﻿using MediatR;
using S.Eartag.Domain;
using S.Eartag.Domain.Model;
using S.Eartag.Infrastructure.Impl;
using S.Eartag.Infrastructure.Interface;
using S.Eartag.Read.API.Application.Command.SheepFileConditionCommand;

namespace S.Eartag.Read.API.Application.CommandHandlers.SheepFileConditionCommandHandler
{
    public class SheepFileConditionGetCommandHandler : IRequestHandler<SheepFileConditionGetCommand, PageInfoS<SheepFile>>
    {
        private readonly ISheepFileRepository sheepFileRepository;

        public SheepFileConditionGetCommandHandler(ISheepFileRepository sheepFileRepository)
        {
            this.sheepFileRepository = sheepFileRepository;
        }

        public async Task<PageInfoS<SheepFile>> Handle(SheepFileConditionGetCommand request, CancellationToken cancellationToken)
        {
            var list = sheepFileRepository.All().Result.Where(x => !x.IsDel).AsQueryable();
            var res = new SheepFile();
            if (!string.IsNullOrEmpty(request.earNumber))
            {
                list = list.Where(x => x.EarNumber.Contains(request.earNumber));
            }
            if (!string.IsNullOrEmpty(request.electronicearnumber))
            {
                list = list.Where(x => x.ElectronicEarNumber.Contains(request.electronicearnumber));
            }
            if (request.breed>0)
            {
                list = list.Where(x => x.breed == request.breed);
            }
            if (request.sex>0)
            {
                list = list.Where(x => x.sex == request.sex);
            }
            if (!string.IsNullOrEmpty(request.startdate))
            {
                list = list.Where(x => x.Birthday>=DateTime.Parse(request.startdate));
            }
            if (!string.IsNullOrEmpty(request.enddate))
            {
                list = list.Where(x => x.Birthday <=DateTime.Parse(request.enddate).AddDays(1));
            }
            if (request.minage > 0)
            {
                list = list.Where(x => x.AgeMoon>=request.minage);
            }
            if (request.maxage > 0)
            {
                list = list.Where(x => x.AgeMoon<=request.maxage);
            }
            if (request.growthStage > 0)
            {
                list = list.Where(x => x.growthStage == request.growthStage);
            }
            if (request.cottage!=null)
            {
                list = list.Where(x => x.Cottage ==request.cottage);
            }
            if (request.field!=null)
            {
                list = list.Where(x => x.Field == request.field);
            }
            if (request.sheepType > 0)
            {
                list = list.Where(x => x.sheepType == request.sheepType);
            }
            if (request.geneClass > 0)
            {
                list = list.Where(x => x.geneClass == request.geneClass);
            }
            if (request.productionGrade > 0)
            {
                list = list.Where(x => x.productionGrade == request.productionGrade);
            }
            if (!string.IsNullOrEmpty(request.fatherhorn))
            {
                list = list.Where(x =>x.FatherHorn.Contains(request.fatherhorn));
            }
            if (!string.IsNullOrEmpty(request.femalehorn))
            {
                list = list.Where(x => x.FemaleHorn.Contains(request.femalehorn));
            }
            if (request.presence > 0)
            {
                list = list.Where(x => x.presence ==request.presence);
            }
            if (request.minstatusdays >= 0)
            {
                list = list.Where(x => x.StatusDays >= request.minstatusdays);
            }
            if (request.maxstatusdays >= 0)
            {
                list = list.Where(x => x.StatusDays <= request.maxstatusdays);
            }
            if (request.salesGrade>0)
            {
                list = list.Where(x => x.salesGrade == request.salesGrade);
            }
            if (request.reproductiveState > 0)
            {
                list = list.Where(x => x.reproductiveState == request.reproductiveState);
            }
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling((totalCount * 1.0) / request.psize);
            list = list.OrderByDescending(x => x.Id).Skip((request.pindex - 1) * request.psize).Take(request.psize);
            return  new PageInfoS<SheepFile>()
            {
                list=list.ToList(),
                totalCount=totalCount,
                pageCount=pageCount,
            };
        }
    }
}
